<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<script type="text/javascript" charset="utf-8">
var admin_user_datagrid;
var admin_user_editRow = undefined;
var admin_user_addRow = undefined;
var admin_user_searchForm = undefined;
$(function(){

	admin_user_searchForm = $('#admin_user_searchForm').form();
	  $("#admin_user_searchButton").bind('click', function(){   
		  
		  admin_user_search(); 
	    }); 
	  $("#admin_user_CleanSearchButton").bind('click', function(){   
		  
		  admin_user_cleanSearch();
    }); 
		
	  admin_user_datagrid=$('#admin_user_datagrid').datagrid({
		url:'${pageContext.request.contextPath}/userAction!DataGrid.action',
		title:'用户信息查询',
		iconCls:'icon-search',
		toolbar : '#admin_user_toolbar',
		fit : true,
		border : false,
		striped:true,
		idField : 'id',
		rownumbers: true,		//显示行号
		fitColumns : true,		//显示横向滚动条
		pagination : true,
		pageSize : 20,
		pageList : [ 10,20, 30, 40, 50 ],
		sortName:'cjsj',
		sortOrder:'desc',
		frozenColumns:[[
				{
					title:'编号',
					field:'id',
					width:100,
					checkbox : true,
					sortable : true
				},
				{
					title:'用户名',
					field:'user',
					width:100,
					sortable:true,
					editor:{
						type:'validatebox',
						options:{
							required:true
						}
					}
				}
			]],
			columns:[[
				{
					title:'密码',
					field:'password',
					width:100,
					formatter:function(value){
						return "******";
					}
				},
				{
					title:'所属组',
					field:'userGroupList',
					width:100,
					sortable:true,
					formatter:function(value){
						if(value){
							return value[0];
						}
					},
					editor:{
						type:'comboboxFk',
						options:{
							url : '${pageContext.request.contextPath}/userGroupAction!comboboxInfo.action',
							valueField : 'id',
							textField : 'userGroupName',
							editable:false,
							required:true
						}
					}
				},
				{
					title:'用户姓名',
					field:'name',
					sortable:true,
					width:100,
					editor:{
						type:'text',
					}
				},{
					title:'用户描述',
					field:'userDescription',
					sortable:true,
					width:100,
					editor:{
						type:'text',
					}
				},
				{
					title:'创建时间',
					field:'cjsj',
					width:100,
					sortable:true,
				}
			]],
		
			onAfterEdit : function(rowIndex, rowData, changes) {
				var inserted = admin_user_datagrid.datagrid('getChanges', 'inserted');
				var updated = admin_user_datagrid.datagrid('getChanges', 'updated');
				if (inserted.length < 1 && updated.length < 1) {
					admin_user_editRow = undefined;
					admin_user_addRow = undefined;
					admin_user_datagrid.datagrid('unselectAll');
					return;
				}
				var url = '';
				if (inserted.length > 0) {
					url = '${pageContext.request.contextPath}/userAction!addUser.action';
				}
				if (updated.length > 0) {
					url = '${pageContext.request.contextPath}/userAction!update.action';
				}
				$.ajax({
					url : url,
					data : rowData,
					dataType : 'json',
					type:'POST',
					success : function(r) {
						if (r.success) {
							admin_user_datagrid.datagrid('acceptChanges');
							$.messager.show({
								msg : r.msg,
								title : '成功'
							});
							admin_user_editRow = undefined;
							admin_user_addRow = undefined;
							admin_user_datagrid.datagrid('load');
						} else {
							admin_user_datagrid.datagrid('rejectChanges');
							$.messager.alert('错误', r.msg, 'error');
						}
						admin_user_datagrid.datagrid('unselectAll');
						admin_user_editRow = undefined;
						admin_user_addRow = undefined;
					}
				});
			},
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#admin_user_easyui-menu').menu('show', {
					center : e.pageX,
					top : e.pageY
				});
			},
			onDblClickRow : function(rowIndex, rowData) {
				if(admin_user_addRow!=undefined){
					$.messager.show({
						msg : "请先保存或撤销编辑！",
						title : '提示',
					});
					return;
				}else{
					if (admin_user_editRow != undefined) {
						admin_user_datagrid.datagrid('endEdit', admin_user_editRow);
					}

					if (admin_user_editRow == undefined) {
						
						admin_user_datagrid.datagrid('beginEdit', rowIndex);
						admin_user_editRow = rowIndex;
						admin_user_datagrid.datagrid('unselectAll');
					}
				}
				
			}
			
			});
			
		});
		
		function admin_user_appendColumns(){
			
			if (admin_user_editRow != undefined||admin_user_addRow!=undefined) {
				$.messager.show({
					msg : "请先保存或撤销编辑！",
					title : '提示',
				});
				return;
			}
		
			else{
				admin_user_datagrid.datagrid('unselectAll');
		
		
				var row = {};
				admin_user_datagrid.datagrid('insertRow', {
					index : 0,
					row : row
				});
				admin_user_addRow = 0;
				admin_user_datagrid.datagrid('selectRow', admin_user_addRow);
				admin_user_datagrid.datagrid('beginEdit', admin_user_addRow);
			}
		}
		
		function admin_user_save(){
			if (admin_user_editRow != undefined) {
					admin_user_datagrid.datagrid('endEdit', admin_user_editRow);
					admin_user_editRow = undefined;
			}
			if (admin_user_addRow != undefined) {
					admin_user_datagrid.datagrid('endEdit', admin_user_addRow);
					admin_user_datagrid.datagrid('getChanges', 'inserted');
				
			}
		}

		function admin_user_edit() {
			var rows = admin_user_datagrid.datagrid('getSelections');
			if (rows.length == 1) {
				if(admin_user_addRow!=undefined){
					$.messager.show({
						msg : "请先保存或撤销编辑！",
						title : '提示',
					});
					return;
				}else{
					if (admin_user_editRow != undefined) {
						admin_user_datagrid.datagrid('endEdit', admin_user_editRow);
					}
					if (admin_user_editRow == undefined) {
						
						admin_user_editRow = admin_user_datagrid.datagrid('getRowIndex', rows[0]);
						admin_user_datagrid.datagrid('beginEdit', admin_user_editRow);
						admin_user_datagrid.datagrid('unselectAll');
					}
				}
				
			} else {
				$.messager.show({
					msg : '请选择一项进行修改！',
					title : '错误'
				});
			}
		}
		function admin_user_search() {
			if(admin_user_editRow != undefined||admin_user_addRow!=undefined){
				$.messager.show({
					msg : '请先保存或撤销编辑！',
					title : '提示'
				});
			}else{
				admin_user_datagrid.datagrid('load', serializeObject(admin_user_searchForm));
			}
			
		}
		function admin_user_cleanSearch() {
			admin_user_editRow = undefined;
			admin_user_addRow=undefined;
			admin_user_datagrid.datagrid('load', {});
			admin_user_searchForm.find('input').val('');
		}
		function admin_user_del() {
			if (admin_user_editRow != undefined||admin_user_addRow!=undefined) {
				$.messager.show({
					msg : "请先保存或撤销编辑！",
					title : '提示',
				});
				return;
			}
			var rows = admin_user_datagrid.datagrid('getSelections');
			var ids = [];
			if (rows.length > 0) {
				$.messager.confirm('请确认', '您要删除当前所选项目？', function(r) {
					if (r) {
						for ( var i = 0; i < rows.length; i++) {
							ids.push(rows[i].id);
						}
						$.ajax({
							url : '${pageContext.request.contextPath}/userAction!delete.action',
							data : {
								ids : ids.join(',')
							},
							type:'POST',
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									admin_user_datagrid.datagrid('acceptChanges');
									$.messager.show({
										msg : r.msg,
										title : '成功'
									});
									admin_user_editRow = undefined;
									admin_user_addRow = undefined;
									admin_user_datagrid.datagrid('clearSelections');
									admin_user_datagrid.datagrid('load');
								} else {
									admin_user_datagrid.datagrid('rejectChanges');
									$.messager.alert('错误', r.msg, 'error');
								}
							}
						});
					}
				});
			} else {
				$.messager.alert('提示', '请选择要删除的记录！', 'error');
			}
		}
function admin_user_unedit(){
	admin_user_datagrid.datagrid('unselectAll');
	admin_user_datagrid.datagrid('rejectChanges');
	admin_user_editRow = undefined;
	admin_user_addRow = undefined;
}
</script>
<div class="easyui-layout" fit="true">
	<div region="center" border="false" style="overflow: hidden;">
		<div id="admin_user_toolbar" class="datagrid-toolbar" style="height: auto;display: none;">
			<fieldset>
				<legend>筛选</legend>
					<form id="admin_user_searchForm">
						<table style="width: 100%;height: 100%;"  class="datagrid-toolbar">
							<tr>
								<td><label>用户名:&nbsp</label><input name="user" id="user"/>
								
								<label>创建时间:&nbsp</label><input name="startCjsj" id="startCjsj" editable="fasle" class="easyui-datetimebox"/>至<input name="endCjsj" id="endCjsj" editable="fasle" class="easyui-datetimebox"/>
									<a href="javascript:void(0);" class="easyui-linkbutton" onclick="admin_user_search()">查询</a>
									<a href="javascript:void(0);" class="easyui-linkbutton" onclick="admin_user_cleanSearch()">清空</a>
								</td>
							</tr>
						</table>
					</form>
			</fieldset>
			<div>
				<a class="easyui-linkbutton" iconCls="icon-add" onclick="admin_user_appendColumns();" plain="true" href="javascript:void(0);">增加</a> 
				<a class="easyui-linkbutton" iconCls="icon-remove" onclick="admin_user_del();" plain="true" href="javascript:void(0);">删除</a> 
				<a class="easyui-linkbutton" iconCls="icon-edit" onclick="admin_user_edit();" plain="true" href="javascript:void(0);">编辑</a> 
				<a class="easyui-linkbutton" iconCls="icon-save" onclick="admin_user_save();" plain="true" href="javascript:void(0);">保存</a> 
				<a class="easyui-linkbutton" iconCls="icon-undo" onclick="admin_user_unedit();" plain="true" href="javascript:void(0);">取消编辑</a> 
				<a class="easyui-linkbutton" iconCls="icon-undo" onclick="admin_user_datagrid.datagrid('unselectAll');" plain="true" href="javascript:void(0);">取消选中</a>
			</div>
		</div>
		<table id="admin_user_datagrid"></table>
	</div>
</div>
